package service;

import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleIfStatement;
import domain.Diary;
import domain.Item;
import domain.Product;
import persistence.DiaryDAO;
import persistence.Impl.DiaryDAOImpl;
import persistence.ItemDAO;

import java.util.List;

/**
 * @projectName: petStore
 * @package: service
 * @className: DialogService
 * @author: suqianbei
 * @description: TODO
 * @date: 2021/11/3 15:29
 * @version: 1.0
 */
public class DialogService {
    private DiaryDAO diaryDAO;
    private CatelogService catelogService;

    public DialogService() {
        diaryDAO = new DiaryDAOImpl();
        catelogService=new CatelogService();
    }

    public List<Diary> getAllDialog() {
        return diaryDAO.getAllDiary();
    }

    public List<Diary> getDiaryByName(String name) {
        return diaryDAO.getDiaryByName(name);
    }

    // TODO enable using more than one keyword
    public List<Diary> searchDiaryList(String name,String keyword) {
        return diaryDAO.getDiaryByKeyword(name,keyword.toLowerCase());
    }
    /**
     * @param name
     * @param date
     * @param categoryId
     * @param productId
     * @param itemId
     * @param state
     * @param最后为1即为加入购物车 0为visit -1从购物车删除 2为登录 3为退出 4为查看购物车
     */
    public void insertNewDiary(String name, String date, String categoryId, String productId, String itemId, String state) {
        Diary diary = new Diary();
        diary.setName(name);
        diary.setDate(date);
        String action =state.replaceFirst("/","");
//        if (state == "/addItemToCart") {
//            action = "add to cart ";
//        }
//        else if(state == "/viewCategory"||state=="/viewProduct"||state=="/viewItem") action="visit ";
//        else if (state=="/ViewIncludeAccountFields")action="log in ";
//        else if (state=="/SignOut")action="log out ";
//        else if (state=="/removeItemFromCart")action="Delete from Cart";
//        else {
//            action=""
//        }
        if (itemId != null) {
            action = action+"-"+catelogService.getProduct(catelogService.getItem(itemId).getProductId()).getCategoryId() + "-" + catelogService.getItem(itemId).getProductId() + "-" + itemId;
        } else {
            if (productId != null) {
                action = action+"-"+catelogService.getProduct(productId).getCategoryId() + "-" +productId;
            } else {
                if(categoryId!=null)
                action = action+"-"+categoryId;
            }
        }
        diary.setAction(action);
        diaryDAO.insertNewDiary(diary);
    }
}
